from sqlalchemy.orm import Session

from app.admin_service.service import account_service
from app.admin_service.schemas.session import SessionResponse
from common.config.auth import verify_password, create_token, create_payload
from common.middleware.exceptions import AppException


def create(db: Session, username: str, password: str) -> SessionResponse:
    # 1. 校验账户是否存在
    account = account_service.get_account_by_name(db, username)

    # 2. 校验密码是否正确
    if verify_password(password, account.password):
        # 3. 返回Session信息
        return SessionResponse(
            access_token=create_token(create_payload(account.account_name)),
            token_type="Bearer"
        )
    else:
        raise AppException("密码错误")



